package com.tiger.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

/**
 * PnsPush.
 *
 */
public class PnsPush {
	private static final Logger logger = LoggerFactory.getLogger(PnsPush.class);

	private static final String PNS_API = Conf.get("pns.api");

	public static boolean alert(String alert, String... mobileNumbers) {
		if (StringUtils.isEmpty(alert) || alert.startsWith("{")) {
			logger.error("alert is empty or alert is json. alert={}", alert);
			return false;
		}
		return sendMessage(alert, mobileNumbers);
	}

	public static boolean sendMessage(Object message, String... mobileNumbers) {
		return sendMessage(Json.toJson(message), mobileNumbers);
	}

	public static boolean sendMessage(String message, String... mobileNumbers) {
		String pnsPushApi = getPnsPushApi();
		String mobiles = String.join(",", mobileNumbers);
		if (pnsPushApi == null) {
			return false;
		} else {
			Http.post(pnsPushApi, "content", message, "mobile", mobiles);
			return true;
		}
	}

	private static String getPnsPushApi() {
		if (StringUtils.isEmpty(PNS_API)) {
			logger.error("PNS_API url is not config");
			return null;
		} else {
			return PNS_API + "/push";
		}
	}

}
